<template>
    <div class="app-container">
        <div class="order_stat">
            <div class="title"><span>分类</span>
                <el-button type="primary" size="mini" @click="handleAdd">添加分类</el-button>
            </div>
        </div>
        <div class="table">
            <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:good:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:good:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:good:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:good:export']">批量导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

            <el-table v-loading="loading" :data="goodList" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column label="编号" align="center" prop="deptName" />

                <el-table-column label="名称" align="center" prop="deptName" />
                <el-table-column label="兑换类型" align="center" prop="sort" />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope='{row}'>
                        {{ row.status == 0 ? '正常' : '停用' }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="delFlag">
                    <template slot-scope='{row}'>
                        {{ row.delFlag == 0 ? '显示' : '隐藏' }}
                    </template>
                </el-table-column>
                <el-table-column width="180" label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"
                           >禁用</el-button>
                        <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                           >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>


        <!-- 添加分类对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="分类名称" prop="deptName">
                    <el-input v-model="form.deptName" placeholder="请输入分类名称" />
                </el-form-item>
                <el-form-item label="是否启用" prop="status">
                    <el-switch v-model="form.status">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { goodClassifyList, goodClassifyDetail, goodclassifyAdd, goodclassifyEdit, goodclassifyDelete } from "@/api/system/goods/classify";

export default {
    name: "Good",
    data() {
        return {
            order_module: [
                { name: '累计余额', num: 1939376.64, toolip: '累计所有平台产生的余额' },
                { name: '已使用', num: 1939376.64, toolip: '使用余额支付或使用余额抵扣统计' },
                { name: '会员账户余额', num: 1939376.64, toolip: '所有会员现有账户的余额' },
                { name: '已提现余额', num: 1939376.64, toolip: '已提现的账户累计余额' },
            ],
            checkboxList: '',
            activeName: 'first',
            // 日期范围
            dateRange: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 供应商商品表格数据
            goodList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deptName: null,
                sort: null,
                classifyImg: null,
                status: null,
                delFlag: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                productLevel: [
                    { required: true, message: "商品等级 1vip 3共享店主 4联创 5区域运营 6总裁不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询供应商商品列表 */
        getList() {
            this.loading = true;
            // this.addDateRange(this.queryParams, this.dateRange)
            goodClassifyList(this.queryParams).then(response => {
                this.goodList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deptName: null,
                sort: null,
                classifyImg: null,
                status: null,
                delFlag: null,
            };
            this.dateRange = [];
            this.resetForm("form");
        },
        uploadImg(v) {
            this.form.classifyImg = v
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加商品分类";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            goodClassifyDetail(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改商品分类";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.status = this.form.status ? '0' : '1'
                    this.form.delFlag = this.form.delFlag ? '0' : '1'
                    if (this.form.id != null) {
                        goodclassifyEdit(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        goodclassifyAdd(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除数据？').then(function () {
                return goodclassifyDelete(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/good/export', {
                ...this.queryParams
            }, `good_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style lang="less" scoped>
.app-container {
    width: 100%;
    height: 1005;
    background-color: #f8f8f8;

    .order_stat {
        background: #FFF;
        padding: 10px 10px;
        margin-bottom: 10px;

        .title {
            font-size: 18px;
            font-weight: bold;
            display: flex;
            align-items: center;
            height: 40px;
            display: flex;
            justify-content: space-between;
        }

    }

    .table {
        width: 100%;
        background-color: #fff;
        padding:15px 10px ;
    }

}
</style>